package de.tum.beilmaier.im.server.dao.impl;

import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;

import de.novanic.eventservice.client.event.domain.DomainFactory;
import de.novanic.eventservice.service.RemoteEventServiceServlet;
import de.tum.beilmaier.im.client.MessagingServiceAsync;
import de.tum.beilmaier.im.server.MessagingServiceImpl;
import de.tum.beilmaier.im.server.dao.UserDao;
import de.tum.beilmaier.im.shared.model.User;

public class UserDaoImpl extends RemoteEventServiceServlet implements UserDao {

	public static UserDaoImpl INSTANCE = new UserDaoImpl();
	private LinkedHashMap<String, User> allUsers = null;
	private LinkedHashMap<String, User> loggedInUsers = null;
	private String contextPath = null;
	private UserDaoImpl()
	{
		allUsers = new LinkedHashMap<String,User>();
		loggedInUsers = new LinkedHashMap<String,User>();
		//insertDummyData();
		//writeUsertoXML();
		//readUsersfromXML();
	}
	
	private void readUsersfromXML() {
		
		XMLDecoder decoder = null;
		try {
			decoder = new XMLDecoder(new BufferedInputStream(
			        new FileInputStream(contextPath+"users.xml")));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		LinkedHashMap<String, User> allUsersfromXML = (LinkedHashMap<String, User>) decoder.readObject();
		decoder.close();	
		
		Set<Entry<String, User>> allUsersfromXMLSet = allUsersfromXML.entrySet();
		for (Entry<String, User> user : allUsersfromXMLSet)
		{
		user.getValue().setUserDomain(DomainFactory.getDomain(user.getValue().getUserId()));
		User newUser = new User(user.getValue().getUserId(), user.getValue().getPassword(), user.getValue().getVorname(), user.getValue().getName());
		allUsers.put(newUser.getUserId(),newUser);
		}
	}

	private void writeUsertoXML() {
	
			XMLEncoder encoder = null;
			try {
				encoder = new XMLEncoder(new BufferedOutputStream(
				        new FileOutputStream("users.xml")));
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		        encoder.writeObject(allUsers);
		        encoder.close();
			
}

	private void insertDummyData() {
		User franz = new User("franz", "password", "franz", "beilmeier");
		User hubert = new User("hubert", "password", "hubert", "mueller");
		User heino = new User("heino", "password", "heino", "hanswurst");
		User helmut = new User("helmut", "password", "helmut", "kohl");
		allUsers.put(franz.getUserId(), franz);
		allUsers.put(hubert.getUserId(), hubert);
		allUsers.put(heino.getUserId(), heino);
		allUsers.put(helmut.getUserId(), helmut);
		
		
		
	}

	public static UserDaoImpl getInstance()
	{
	return INSTANCE;	
	}
	
	@Override
	public User getUserbyIdAndPassword(String userid, String password) {
		User loginuser = allUsers.get(userid);
		if (loginuser != null)
		{
			if (loginuser.getPassword().equals(password))
			{
			loggedInUsers.put(loginuser.getUserId(), loginuser);	
			return loginuser;	
			}
		}
		return null;
	}

	@Override
	public LinkedHashMap<String, User> getAllUser() {
		return allUsers;
	}

	@Override
	public LinkedHashMap<String, User> getLoggedInUser() {
		return loggedInUsers;
	}

	@Override
	public void addUser(User user) {
		allUsers.put(user.getUserId(), user);
		writeUsertoXML();

	}

	@Override
	public void removeUser(User user) {
	allUsers.remove(user.getUserId());
}

	public void logoutUser(User logoutUser) {
		System.out.println("userDao logout user "+logoutUser.getUserId());
		logoutUser.removeAllChatRooms();
		loggedInUsers.remove(logoutUser.getUserId());
	}

	public void setContextPath(String contextPath) {
		this.contextPath = contextPath;
		readUsersfromXML();
		
	}

}
